const Router = require('koa-router');
const config = require('../config');

const emailRouter = require('./email');
const statusRouter = require('./status');
const contentRouter = require('./content');
const keywordRouter = require('./keyword');
const siteRouter = require('./site');

const router = new Router();

router.post('/api/login', async(ctx, next) => {
    let body = ctx.request.body;
    if (!body.password || body.password !== config.password) {
        return ctx.send({
            code: 400,
            msg: 'password is wrong'
        });
    }
    ctx.session.isLogin = true;
    ctx.send({
        code: 200,
        msg: 'login success'
    })
});
router.all('/api/*', async(ctx, next) => {
    if (ctx.session && ctx.session.isLogin) {
        await next()
    } else {
        ctx.send({
            code: 401,
            msg: 'no login'
        });
    }
})

router.use('/api', emailRouter.routes());
router.use('/api', statusRouter.routes());
router.use('/api', contentRouter.routes());
router.use('/api', keywordRouter.routes());
router.use('/api', siteRouter.routes());

module.exports = router;